//
//  WeiBoTableView.m
//  CloudWeibo
//
//  Created by CLOUD on 12/5/14.
//  Copyright (c) 2014 Cloud. All rights reserved.
//

#import "WeiBoTableView.h"
#import "WeiBoTableViewCell.h"
#import "WeiBoModel.h"
#import "WeiBoView.h"
#import "DetailViewController.h"


@implementation WeiBoTableView

- (id)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self) {
        [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reloadData) name:kReloadWeiBoTableViewByBrowseMode object:nil];
    }
    return self;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.data.count;
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identitfy = @"WeiBoCell";
    WeiBoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identitfy];
    if (cell == nil) {
        cell = [[[WeiBoTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identitfy] autorelease];
    }
    WeiBoModel *weiBo = [self.data objectAtIndex:indexPath.row];
    //NSLog(@"d%d", indexPath.row);
    cell.weiBoModel = weiBo;
    return cell;
}

- (float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WeiBoModel *weiBo = [self.data objectAtIndex:indexPath.row];
    float height = [WeiBoView getWeiboViewHeight:weiBo isRepost:NO isDetail:NO];
    height += 60;
    return height;
}

- (void) tableView:(BaseTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    WeiBoModel *weiBo = [self.data objectAtIndex:indexPath.row];
    DetailViewController *detail = [[DetailViewController alloc] init];
    detail.weiBoModel = weiBo;
    [self.getViewController.navigationController pushViewController:detail animated:YES];
    [detail release];
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}


@end
